<?php
/**
 * 用户列表
 */

$title = '用户管理列表';
include "header.php";

$sum = 8; #每页数量
$page = shop::page($sum, $_QET['page']); #取出翻页数据
$user_list = user::user_list($sum, $page['count'], $page['limit'], $_QET['id'], (int)$_QET['grid']); #获取商品列表
$paging = shop::paging($user_list['count'], $_QET['page']); #取出翻页按钮数据
if (!empty($_QET['unset'])) {
    if (empty($_QET['unset'])) show_msg('温馨提示', '请填写完整！', '2');
    $a = $DB->query("DELETE FROM `sky_user` WHERE (`id`='{$_QET['unset']}')"); #删除
    if ($a) {
        show_msg('成功提示', '编号:' . $_QET['unset'] . '删除成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['unset'] . '删除失败,原因->' . $DB->error(), '2');
}


if (!empty($_QET['reset'])) {
    if (empty($_QET['reset'])) show_msg('温馨提示', '请填写完整！', '2');
    $password = md5(123456);
    $a = $DB->query("UPDATE `sky_user` SET `password`='$password' WHERE (`id`='$id')"); #修改
    if ($a) {
        show_msg('成功提示', '编号:' . $_QET['id'] . '密码成功重置为123456！,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['id'] . '密码重置失败,原因->' . $DB->error(), '2');
}

if (isset($_QET['money'])) { #积分加款
    if (!(float)isset($_QET['price']) || empty($_QET['type'])) show_msg('温馨提示', '请填写完整！', '2');
    $id = (int)$_QET['money'];
    $DB = \Medoo\DB\SQL::DB();
    $User = $DB->get('user', '*', [
        'id' => $id
    ]);
    if (!$User) show_msg('温馨提示', '用户' . $id . '不存在,请核对后再进行操作！');

    if ($_QET['type'] == 1) {
        $SQL = [
            'currency[+]' => (float)$_QET['price'],
        ];
    } else {
        $SQL = [
            'currency[-]' => (float)$_QET['price'],
        ];
    }

    $Res = $DB->update('user', $SQL, [
        'id' => $id
    ]);
    if (!$Res) show_msg('温馨提示', '编号:' . $_QET['id'] . '积分调整失败！', 2);

    if ($_QET['type'] == 1) {
        userlog('积分充值', '站长在后台为用户' . $id . '手动增加了' . (float)$_QET['price'] . '点积分！', $id, (float)$_QET['price']);
        show_msg('成功提示', '成功为用户' . $User['name'] . '增加' . (float)$_QET['price'] . '点积分！', '1');
    } else {
        userlog('积分扣除', '站长在后台为用户' . $id . '手动扣除了' . (float)$_QET['price'] . '点积分！', $id, (float)$_QET['price']);
        show_msg('成功提示', '成功为用户' . $User['name'] . '扣除' . (float)$_QET['price'] . '点积分！', '1');
    }
}
if (isset($_QET['moneys'])) { #余额加款
    if (!(float)isset($_QET['price']) || empty($_QET['type'])) show_msg('温馨提示', '请填写完整！', '2');
    $id = (int)$_QET['moneys'];
    $DB = \Medoo\DB\SQL::DB();
    $User = $DB->get('user', '*', [
        'id' => $id
    ]);
    if (!$User) show_msg('温馨提示', '用户' . $id . '不存在,请核对后再进行操作！');

    if ($_QET['type'] == 1) {
        $SQL = [
            'money[+]' => (float)$_QET['price'],
        ];
    } else {
        $SQL = [
            'money[-]' => (float)$_QET['price'],
        ];
    }

    $Res = $DB->update('user', $SQL, [
        'id' => $id
    ]);
    if (!$Res) show_msg('温馨提示', '编号:' . $_QET['id'] . '余额调整失败！', 2);

    if ($_QET['type'] == 1) {
        userlog('后台加款', '站长在后台为用户' . $id . '手动加款了' . (float)$_QET['price'] . '元！', $id, (float)$_QET['price']);
        show_msg('成功提示', '成功为用户' . $User['name'] . '加款' . (float)$_QET['price'] . '元！', '1');
    } else {
        userlog('后台扣款', '站长在后台为用户' . $id . '手动扣除了' . (float)$_QET['price'] . '元！', $id, (float)$_QET['price']);
        show_msg('成功提示', '成功为用户' . $User['name'] . '扣款' . (float)$_QET['price'] . '元！', '1');
    }
}
if (!empty($_QET['state'])) {
    $a = $DB->get_row("SELECT * FROM `sky_user` WHERE `id` = '{$_QET['state']}' LIMIT 1 ");
    if ($a['state'] == 1) {
        $Tips = '禁封';
        $b = 2;
    } else {
        $Tips = '解封';
        $b = 1;
    }
    $b = $DB->query("UPDATE `sky_user` SET `state`='$b' WHERE (`id`='{$_QET['state']}')"); #删除
    if ($b) {
        show_msg('成功提示', '编号:' . $_QET['state'] . $Tips . '成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['state'] . '修改失败,原因->' . $DB->error(), '2');
}
if (!empty($_QET['uidup'])) {
    if (empty((int)$_QET['uidup']) || empty((int)$_QET['up'])) show_msg('温馨提示', '请填写完整！', '2');
    $a = $DB->query("UPDATE `sky_user` SET `grade`='{$_QET['up']}'  WHERE (`id`='{$_QET['uidup']}')"); #删除
    if ($a) {
        userlog('等级变化', '站长在后台将您的用户等级设置为：' . $_QET['up'], $_QET['uidup']);
        show_msg('成功提示', '编号:' . $_QET['uidup'] . '等级设置成功,请点击下方按钮返回', '1');
    } else show_msg('温馨提示', '编号:' . $_QET['uidup'] . '等级设置失败,原因->' . $DB->error(), '2');
}

$Res = $DB->query("SELECT * FROM `sky_price`");
$data_gr = [];
while ($Rest = $DB->fetch($Res)) {
    $data_gr[] = [
        'sort' => $Rest['sort'],
        'name' => $Rest['name'],
    ];
}
?>
<!-- third party css -->
<link href="<?= $cdnserver == null ? '../' : $cdnserver ?>assets/css/vendor/dataTables.bootstrap4.css" rel="stylesheet"
      type="text/css"/>
<link href="<?= $cdnserver == null ? '../' : $cdnserver ?>assets/css/vendor/responsive.bootstrap4.css" rel="stylesheet"
      type="text/css"/>
<!-- third party css end -->
<!-- App css -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="row mb-2">
                    <div class="col-sm-4">
                        <a href="javascript:query()" class="btn btn-danger mb-2"><i
                                    class="layui-icon layui-icon-add-circle-fine mr-2"></i>
                            查找用户
                        </a>
                    </div>
                </div>
                <div class="layui-card">
                    <div class="layui-card-header layui-text">
                        <?php foreach ($data_gr as $value) {
                            echo '<a href="?grid=' . $value['sort'] . '">' . $value['name'] . '</a> - ';
                        }
                        echo '<a href="javascript:layer.alert(\'共 ' . $DB->count("SELECT sum(money) FROM `sky_user`") . ' 元\',{title:\'用户余额总数\'})">总余额</a>';
                        ?>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-centered table-striped dt-responsive nowrap w-100"
                           id="products-datatable">
                        <thead>
                        <tr style="white-space: nowrap">
                            <th>ID</th>
                            <th>KPID</th>
                            <th>名称</th>
                            <th>头像</th>
                            <th>登陆账号</th>
                            <th>用户订单</th>
                            <th>绑定手机号</th>
                            <th>用户域名</th>
                            <th>用户等级</th>
                            <th>用户日志</th>
                            <th>上级编号</th>
                            <th>余额</th>
                            <th>剩余积分</th>
                            <th>登陆IP</th>
                            <th>用户状态</th>
                            <th>最近登陆</th>
                            <th>注册时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($user_list['data'] as $v) {
                            ?>
                            <tr style="white-space: nowrap;font-size: 0.9em">
                                <td>
                                    <a href="admin.order.list.php?uid=<?= $v['id'] ?>" title="查看该用户的订单"
                                       class="badge badge-info-lighten">
                                        <?= $v['id'] ?>
                                    </a>
                                </td>
                                <td class="table-user">
                                    <a href="javascript:layer.alert('<?= $v['user_idu'] ?>',{icon:1,title:'用户唯一识别码'})">查看</a>
                                </td>
                                <td>
                                    <?= $v['name'] ?>
                                </td>
                                <td>
                                    <img src="<?= $v['image'] ?>" class="rounded-circle bg-warning"
                                         style="width: 1.5em;height: 1.5em"/>
                                </td>
                                <td>
                                    <?= ($v['username'] == '' ? '未设置' : $v['username']) ?>
                                </td>
                                <td>
                                    <a href="admin.order.list.php?uid=<?= $v['id'] ?>" title="查看该用户的订单"
                                       class="badge badge-primary">
                                        Ta的订单
                                    </a>
                                </td>
                                <td>
                                    <?= ($v['mobile'] == '' ? '未绑定' : '<a href="tel:' . $v['mobile'] . '">' . $v['mobile'] . '</a>') ?>
                                </td>
                                <td class="text-primary">
                                    <?= ($v['domain'] == '' ? '未配置' : $v['domain']) ?>
                                </td>
                                <td onclick="update(<?= $v['id'] ?>,<?= $v['grade'] ?>)">
                                    V<?= $v['grade'] ?>密价
                                </td>
                                <td>
                                    <a href="admin.user.log.php?uid=<?= $v['id'] ?>" class="badge badge-danger-lighten">
                                        用户日志
                                    </a>
                                </td>
                                <td>
                                    <span onclick="location.href='admin.user.list.php?id=<?= $v['superior'] ?>'"
                                          class="badge badge-dark-lighten">
                                        <?= $v['superior'] ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-primary-lighten"
                                          onclick="moneys(<?= $v['money'] ?>,<?= $v['id'] ?>)">
                                        <?= $v['money'] ?>元
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-warning"
                                          onclick="money(<?= $v['currency'] ?>,<?= $v['id'] ?>)">
                                        <?= $v['currency'] . $conf['currency'] ?>
                                    </span>
                                </td>
                                <td>
                                    <?= $v['ip'] ?>
                                </td>
                                <td>
                                    <?= $v['state'] ?>
                                </td>
                                <td>
                                    <?= $v['recent_time'] ?>
                                </td>
                                <td>
                                    <?= $v['found_date'] ?>
                                </td>
                                <td>
                                    <a href="javascript:reset(<?= $v['id'] ?>)">重置</a> /
                                    <a href="javascript:login('<?= $v['user_idu'] ?>')">登陆</a>
                                    <a href="javascript:unset(<?= $v['id'] ?>)" class="action-icon"> <i
                                                class="layui-icon layui-icon-delete"></i></a>
                                </td>
                            </tr>
                        <?php } ?>

                        </tbody>
                    </table>
                </div>
                <nav>
                    <ul class="pagination pagination-rounded" style="overflow: auto">
                        <li class="page-item">
                            <a class="page-link"
                               href="?page=1<?= !empty($_QET['id']) ? '&id=' . $_QET['id'] : '' ?><?= !empty($_QET['grid']) ? '&id=' . $_QET['grid'] : '' ?>"
                               aria-label="Previous">
                                <span aria-hidden="true">«</span>
                                <span class="sr-only">Previous</span>
                            </a>
                        </li>
                        <?php foreach ($paging as $v) { ?>
                            <li class="page-item <?= $v['active'] ?>"><a class="page-link"
                                                                         href="?page=<?= $v['number'] ?><?= !empty($_QET['id']) ? '&id=' . $_QET['id'] : '' ?><?= !empty($_QET['grid']) ? '&id=' . $_QET['grid'] : '' ?>"><?= $v['number'] ?></a>
                            </li>
                        <?php } ?>
                        <li class="page-item">
                            <a class="page-link"
                               href="?page=<?= $shop_list['count'] ?><?= !empty($_QET['id']) ? '&id=' . $_QET['id'] : '' ?><?= !empty($_QET['grid']) ? '&id=' . $_QET['grid'] : '' ?>"
                               aria-label="Next">
                                <span aria-hidden="true">»</span>
                                <span class="sr-only">Next</span>
                            </a>
                        </li>
                        <?php if (!empty($_QET['id']) || !empty($_QET['grid'])) { ?>
                            <li class="page-item">
                                <a class="page-link" href="?page=1" aria-label="Nexst">
                                    <span aria-hidden="true">查看全部</span>
                                    <span class="sr-only">Nexst</span>
                                </a>
                            </li>
                        <?php } ?>
                    </ul>
                </nav>
            </div> <!-- end card-body-->
        </div> <!-- end card-->
    </div> <!-- end col -->
</div>
<?php include "bottom.php"; ?>
<script>
    function unset(a) {
        layer.alert('删除后不可撤销，是否确认删除？', {
            icon: 3, title: '温馨提示', btn: ['取消', '确定删除'], btn2: function (layero, index) {
                window.location.href = '?unset=' + a;
            }
        })
    }

    function reset(a) {
        layer.alert('是否要将此用户的密码重置为123456？', {
            icon: 3, title: '温馨提示', btn: ['取消', '确定'], btn2: function (layero, index) {
                window.location.href = '?reset=' + a;
            }
        })
    }

    function login(a) {
        layer.alert('是否要登陆此用户的后台？', {
            icon: 3, title: '温馨提示', btn: ['取消', '确定'], btn2: function (layero, index) {
                window.open('?login=' + a);
            }
        })
    }

    function moneys(money, id) {

        let _this = this;
        let content = `
				<div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
					    <label class="layui-form-label">操作</label>
					    <div class="layui-input-block">
					      <select name="type">
					        <option value="1">加款</option>
					        <option value="2">扣款</option>
					      </select>
					    </div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">金额</label>
						<div class="layui-input-block">
						  <input type="text" name="money" value="" placeholder="请填写金额" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>
			`;
        var ix = layer.open({
            title: '请选择',
            content: content,
            btn: ['确定', '取消'],
            closeBtn: 0,
            shade: [0.8, '#393D49'],
            shadeClose: true,
            btn1: function (layero, index) {
                location.href = '?moneys=' + id + '&price=' + $("input[name='money']").val() + '&type=' + $("select[name='type']").val();
            },
            btn2: function (layero, index) {
                layer.close(ix);
            },
            success: function (layero, index) {
                layui.use(['form'], function () {
                    var form = layui.form;
                    form.render();
                });
            }
        });
    }

    function money(money, id) {
        let _this = this;
        let content = `
				<div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
					    <label class="layui-form-label">操作</label>
					    <div class="layui-input-block">
					      <select name="type">
					        <option value="1">增加</option>
					        <option value="2">减少</option>
					      </select>
					    </div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">积分</label>
						<div class="layui-input-block">
						  <input type="text" name="money" value="" placeholder="请填写积分数，整数" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>
			`;
        var ix = layer.open({
            title: '请选择',
            content: content,
            btn: ['确定', '取消'],
            closeBtn: 0,
            shade: [0.8, '#393D49'],
            shadeClose: true,
            btn1: function (layero, index) {
                location.href = '?money=' + id + '&price=' + $("input[name='money']").val() + '&type=' + $("select[name='type']").val();
            },
            btn2: function (layero, index) {
                layer.close(ix);
            },
            success: function (layero, index) {
                layui.use(['form'], function () {
                    var form = layui.form;
                    form.render();
                });
            }
        });
    }

    function query() {
        layer.prompt({
            formType: 0,
            value: '',
            title: '请填写用户名或ID查找用户!',
        }, function (value, index, elem) {
            location.href = '?id=' + value;
        });
    }

    function update(id, up) {
        layer.prompt({
            formType: 0,
            value: up,
            title: '请输入需要修改的用户等级！',
        }, function (value, index, elem) {
            window.location.href = '?uidup=' + id + '&up=' + value;
            layer.close(index);
        });
    }
</script>
